/*
 * @lc app=leetcode.cn id=104 lang=swift
 *
 * [104] 二叉树的最大深度
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public var val: Int
 *     public var left: TreeNode?
 *     public var right: TreeNode?
 *     public init(_ val: Int) {
 *         self.val = val
 *         self.left = nil
 *         self.right = nil
 *     }
 * }
 */
class Solution104 {
    // 二叉树的最大深度
    // 转化成子树的深度加上当前这一层
    func maxDepth(_ root: TreeNode?) -> Int {
        guard let node = root else {
            return 0
        }
        let leftDep = maxDepth(node.left)
        let rightDep = maxDepth(node.right)
        return max(leftDep, rightDep) + 1
    }
    static func test () {
        let rootNode = TreeNode(1)
        let node3 = TreeNode(3)
        let node2 = TreeNode(2)
        let node4 = TreeNode(4)
        let node5 = TreeNode(5)
        
        rootNode.left = node3
        rootNode.right = node2
        node3.right = node4
        node4.left = node5
       
        
        let result = Solution104().maxDepth(rootNode)
        print(result)
    }
}
// @lc code=end

